<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="展览名称">
        <el-input v-model="queryParams.exhibitsName" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="展览地点">
        <el-input v-model="queryParams.exhibitsAddress" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="queryParams.exhibitsClass" placeholder="请选择" clearable>
          <el-option label="新展预告" value="1"></el-option>
          <el-option label="常设展览" value="2"></el-option>
          <el-option label="专题展览" value="3"></el-option>
          <el-option label="临展特展" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          placeholder="全部"
          v-model="queryParams.date"
          size="small"
          value-format="yyyy-MM-dd"
          type="date"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-button style="float: right;"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
    </el-form>

    <el-table v-loading="loading" :data="noticeList">
      <el-table-column label="封面图" align="center" prop="noticeType" width="150">
        <template slot-scope="scope">
          <img :src="scope.row.exhibitsCoverImg" style="height: 100px;" />
        </template>
      </el-table-column>
      <el-table-column label="展览名称" align="center" prop="exhibitsName" width="200"></el-table-column>
      <el-table-column label="展览地点" align="center" prop="exhibitsAddress" width="200"></el-table-column>
      <el-table-column label="类型" align="center" prop="exhibitsClass" width="100"></el-table-column>
      <el-table-column label="出展时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.exhibitsStarttime + ' ~ ' + scope.row.exhibitsEndtime  }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <span @click="handleDelete(scope.row)" class="red">删除</span>
          <span @click="handleLang(scope.row)" class="sanm">多语言配置</span>
          <span @click="handleUpdate(scope.row)" class="sanm">编辑</span>
          <span @click="handleView(scope.row)" class="sanm">详情</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="封面图片" prop="exhibitsCoverImg">
              <input type="hidden" v-model="form.exhibitsCoverImg" />
              <img v-if="form.exhibitsCoverImg" :src="form.exhibitsCoverImg" style="width:150px;" />
              <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
                <el-button size="small">
                  上传图片
                  <i class="el-icon-upload el-icon--right"></i>
                </el-button>
              </el-upload>
              <div class="tip">仅支持jpg、png、jpeg格式</div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="展览名称" prop="exhibitsName">
              <el-input v-model="form.exhibitsName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="展览地点" prop="exhibitsAddress">
              <el-input v-model="form.exhibitsAddress" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="展览类型" prop="exhibitsClass">
              <el-select v-model="form.exhibitsClass" placeholder="请选择">
                <el-option label="新展预告" value="1"></el-option>
                <el-option label="常设展览" value="2"></el-option>
                <el-option label="专题展览" value="3"></el-option>
                <el-option label="临展特展" value="4"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="出展时间" prop="noticeType">
              <el-radio-group v-model="form.menuType">
                <el-radio label="1">长期</el-radio>
                <el-radio label="2">非长期</el-radio>
              </el-radio-group>
              <el-date-picker v-if="form.menuType==2"
                v-model="daterangeCreateTime"
                size="small"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="前言">
              <el-input type="textarea" :rows="3" v-model="form.exhibitsForeword" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="介绍">
              <editor v-model="form.exhibitsIntroduce" :min-height="192"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="观展说明">
              <el-input type="textarea" :rows="3" v-model="form.exhibitsExplain" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="注意事项">
              <el-input type="textarea" :rows="3" v-model="form.exhibitsAnnouncements" placeholder="请输入" />
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="展览详情" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-form ref="form" label-width="150px">
          <el-form-item label="封面图片">
            <img :src="form.exhibitsCoverImg" style="width:150px;" />
          </el-form-item>
          <el-form-item label="展览名称">
            <span>{{form.exhibitsName}}</span>
          </el-form-item>
          <el-form-item label="展览地点">
            <span>{{form.exhibitsAddress}}</span>
          </el-form-item>
          <el-form-item label="展览类型">
            <span>{{form.exhibitsClass}}</span>
          </el-form-item>
          <el-form-item label="出展时间">
            <span>{{ form.exhibitsStarttime + ' ~ ' + form.exhibitsEndtime  }}</span>
          </el-form-item>
          <el-form-item label="前言">
            <span>{{form.exhibitsForeword}}</span>
          </el-form-item>
          <el-form-item label="介绍">
            <span>{{form.exhibitsIntroduce}}</span>
          </el-form-item>
          <el-form-item label="观展说明">
            <span>{{form.exhibitsExplain}}</span>
          </el-form-item>
          <el-form-item label="注意事项">
            <span>{{form.exhibitsAnnouncements}}</span>
          </el-form-item>

      </el-form>
    </el-dialog>

    <lang ref="lang" />

  </div>
</template>

<script>
import { upload, removeFile } from '@/api/common';
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/show";

export default {
  name: "Notice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        exhibitsCoverImg: [
          { required: true, message: "请上传封面", trigger: "blur" }
        ],
        exhibitsName: [
          { required: true, message: "请输入展览名称", trigger: "blur" }
        ]
      },
      daterangeCreateTime: [],

      viewOpen: false,

      //多语言设置
      langOpen: false,
      langTit: '多语言配置',
      //type 1输入框 2图片上传 3富文本
      langDicts: [
        {id: 2, name: '名称', status: 0, flag: 1, type: 1, val: 'http://www.baidu.com'}
      ],
      setCur: {},
      setOpen: false,
      langs: [
        {id: 1, name: '中文（简体）', val: ''},
        {id: 2, name: '中文（繁体）', val: ''},
        {id: 3, name: '英语（English）', val: ''}
      ]

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then(response => {
        this.noticeList = response.data.list || response.data || [];
        this.total = response.data.total || 0;
        this.loading = false;
      });
    },
    handleLang(row){
      this.$refs.lang.init(row, 'EXHIBITS','展览');
    },
    handleSet(row){
      this.setCur = row;
      this.setOpen = true;
    },
    setSubmit(){
      addNotice(this.form).then(response => {
        this.$modal.msgSuccess("设置成功");
        this.setOpen = false;
        this.getList();
      });
    },
    setCancel(){
      this.setOpen = false;
    },
    handleView(row){
      this.form = row;
      this.viewOpen = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        exhibitsCoverImg: ''
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增展览";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

        this.form = row;
        this.open = true;
        this.title = "编辑展览";

    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateNotice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.exhibitsStarttime = this.daterangeCreateTime[0] || '';
            this.form.exhibitsEndtime = this.daterangeCreateTime[1] || '';
            addNotice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.id || this.ids
      this.$modal.confirm('是否确认删除？').then(function() {
        return delNotice(noticeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 覆盖默认的上传行为
    // 覆盖默认的上传行为
    requestUpload(e) {
      let formData = new FormData();
      formData.append('file', e.file);
      formData.append('path', 'user/info');
      upload(formData).then(res => {
        if(res.code=='00000'){
          this.form.exhibitsCoverImg = res.data.filePath;
          this.$modal.msgSuccess("上传成功");
        }
        else{
          this.$modal.msgError("上传失败");
        }
      });
    },
    // 上传预处理
    beforeUpload(file) {
      if (file.type.indexOf("image/") == -1) {
        this.$modal.msgError("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。");
        return false
      } else {

      }
    }
  }
};
</script>